import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    currentTab:'/',//默认定位到首页
    tabList:[
    ],
  },
  mutations: {
    updateCurrentTabState(state, current){
      state.currentTab = current
    },
    updateTabListState(state, current){
      let tabs = state.tabList;
      let tab = tabs.find(info=>info.href===current.href);
      if (!tab){
        tabs.push(current);
        state.tabList = tabs;
      }
    },
    removeCurrentTabState(state, href){
      let tabs = state.tabList;
      tabs.forEach((item,index)=>{
        if (item.href === href){
          tabs.splice(index,1);
          if (state.currentTab === item.href){
            let newIndex = index-1;
            if (newIndex>=0){
              let newTab = tabs[index-1];
              state.currentTab = newTab.href;
            }
          }
        }
      })
      state.tabList = tabs;
    }
  },
  actions: {
  },
  modules: {
  }
})
